//
// Created by moonlight on 25-7-25.
//
#include <iostream>
#include <map>
#include <string>
using namespace std;
void printMap(map<int, string> &m) {
    for (map<int, string>::iterator it = m.begin(); it != m.end(); it++) {
        cout << it->first << "-->" << it->second <<" \t";
    }
    cout << endl;
}
void map_constructor() {
    map<int, string> mymap;
    mymap.insert(pair<int, string>(1, "one"));
    mymap.insert(pair<int, string>(2, "two"));
    mymap.insert(pair<int, string>(3, "three"));
    printMap(mymap);
    map<int, string> mymap2(mymap);
    map <int, string> mymap3;
    mymap3 = mymap;
}
void map_func() {
    map<int, string> mymap;
    mymap.insert(pair<int, string>(8, "one"));
    mymap.insert(pair<int, string>(2, "two"));
    mymap.insert(pair<int, string>(3, "three"));
    map<int, string> minemap;
    minemap.insert(pair<int, string>(1, "one"));
    minemap.insert(pair<int, string>(4, "two"));
    minemap.insert(pair<int, string>(3, "three"));
    if (mymap.empty()) {
        cout << "map is empty" << endl;
    }else {
        cout << "map size: " << mymap.size() << endl;
    }
    printMap(mymap);
    mymap.swap(minemap);
    printMap(mymap);
}
void map_function() {
    map<int, string> mymap;
    mymap.insert(pair<int, string>(1, "one"));
    mymap.insert(pair<int, string>(2, "two"));
    mymap.insert(pair<int, string>(3, "three"));
    printMap(mymap);
    mymap.insert(make_pair(9, "one nine"));
    cout<<mymap[2]<<endl;
    mymap.erase(mymap.begin());
    mymap.erase(2);
    mymap.erase(mymap.begin(), mymap.end());
    mymap.clear();
}
void map_search_count() {
    map<int, string> mymap;
    mymap.insert(pair<int, string>(1, "one"));
    mymap.insert(pair<int, string>(2, "two"));
    mymap.insert(pair<int, string>(3, "three"));
    printMap(mymap);
    map<int, string>::iterator iterator = mymap.find(3);
    if (iterator != mymap.end()) {
        cout << iterator->first << "-->" << iterator->second <<" \t";
    }
    else {
        cout<<"Not found"<<endl;
    }
    size_t count = mymap.count(3);
    cout << count << endl;
}
class MyCompare {
public:
    bool operator()(int v1,int v2)const{
        return v1>v2;
    }
};
void map_sort() {
    map<int, string,MyCompare> mymap;
    mymap.insert(pair<int, string>(5, "one"));
    mymap.insert(pair<int, string>(2, "two"));
    mymap.insert(pair<int, string>(9, "three"));
    mymap.insert(pair<int, string>(8, "three"));
    for (map<int, string>::iterator it = mymap.begin(); it != mymap.end(); it++) {
        cout << it->first << "-->" << it->second <<" \t";
    }
}
int main() {
    cout<<"--->> Hello Map! <<---"<<endl;
    map_sort();
    return 0;
}